package com.asurplus.system.controller;


import com.asurplus.common.layui.LayuiTableResult;
import com.asurplus.common.utils.R;
import com.asurplus.system.entity.SysDeptInfo;
import com.asurplus.system.service.SysDeptInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lizhou
 * @since 2020-11-24
 */
@Api(tags = "后台管理端--部门管理")
@Controller
@RequestMapping("/sys/sys-dept-info")
public class SysDeptInfoController {


    @Autowired
    private SysDeptInfoService sysDeptInfoService;

    @GetMapping("init")
    public String init() {
        return "system/deptinfo/list";
    }

    @GetMapping("add")
    public String add(Model model) {
        model.addAttribute("dept", sysDeptInfoService.listDeptInfoTree(0));
        return "system/deptinfo/add";
    }

    @GetMapping("update/{id}")
    public String update(@PathVariable Integer id, Model model) {
        model.addAttribute("data", sysDeptInfoService.getById(id));
        model.addAttribute("dept", sysDeptInfoService.listDeptInfoTree(0));
        return "system/deptinfo/update";
    }

    @GetMapping("list")
    @ResponseBody
    public LayuiTableResult list() {
        List<SysDeptInfo> list = sysDeptInfoService.list();
        return new LayuiTableResult<>(null != list && !list.isEmpty() ? Long.valueOf(list.size()) : 0, list);
    }

    @PostMapping("add")
    @ResponseBody
    public R add(@RequestBody SysDeptInfo sysDeptInfo) {
        return sysDeptInfoService.add(sysDeptInfo);
    }

    @PostMapping("update")
    @ResponseBody
    public R update(@RequestBody SysDeptInfo sysDeptInfo) {
        return sysDeptInfoService.update(sysDeptInfo);
    }

    @GetMapping("updateStatus/{id}/{status}")
    @ResponseBody
    public R updateStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        return sysDeptInfoService.updateStatus(id, status);
    }

    @GetMapping("delete/{id}")
    @ResponseBody
    public R delete(@PathVariable Integer id) {
        return sysDeptInfoService.del(id);
    }

    @GetMapping("listDeptInfoTree/{id}")
    @ResponseBody
    public String listDeptInfoTree(@PathVariable Integer id) {
        return sysDeptInfoService.listDeptInfoTree(id);
    }

    @GetMapping("updateSort/{id}/{sort}")
    @ResponseBody
    public R updateSort(@PathVariable("id") Integer id, @PathVariable("sort") Integer sort) {
        return sysDeptInfoService.updateSort(id, sort);
    }

    @ApiOperation(value = "根据类型获取部门树形下拉选择数据")
    @GetMapping("listDeptInfoTreeSelect")
    @ResponseBody
    public String listDeptInfoTreeSelect() {
        return sysDeptInfoService.listDeptInfoTreeSelect();
    }
}
